export async function base64ToBolb(obj){
	const base64 = obj.trim();	
	try {
		// 提取MIME类型和Base64数据
		const parts = base64.split(';base64,');
		if (parts.length !== 2) {
			throw new Error('Base64格式不正确');
		}
		
		const mimeType = parts[0].split(':')[1];
		const base64Data = parts[1];
		
		// 将Base64转换为二进制
		const byteCharacters = atob(base64Data);
		const byteArrays = [];
		
		for (let offset = 0; offset < byteCharacters.length; offset += 512) {
			const slice = byteCharacters.slice(offset, offset + 512);
			
			const byteNumbers = new Array(slice.length);
			for (let i = 0; i < slice.length; i++) {
				byteNumbers[i] = slice.charCodeAt(i);
			}
			
			const byteArray = new Uint8Array(byteNumbers);
			byteArrays.push(byteArray);
		}
		
		// 创建Blob对象
		const currentBlob = new Blob(byteArrays, { type: mimeType });
		return currentBlob
	} catch (e) {
		console.error(e);
	}
}

function base64ToBlobUrl(base64) {
  // 解析Base64字符串
  const parts = base64.split(';base64,');
  if (parts.length !== 2) {
    throw new Error('Base64格式不正确');
  }
  
  const mimeType = parts[0].split(':')[1];
  const base64Data = parts[1];
  
  // 解码Base64数据
  const byteCharacters = atob(base64Data);
  const byteArrays = [];
  
  for (let offset = 0; offset < byteCharacters.length; offset += 512) {
    const slice = byteCharacters.slice(offset, offset + 512);
    
    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }
    
    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }
  
  // 创建Blob对象并生成URL
  const blob = new Blob(byteArrays, { type: mimeType });
  return URL.createObjectURL(blob);
}
export async function base64ToFile(base64, filename = 'image', lastModified = new Date().getTime()) {
    // 检查base64格式
    if (!base64 || typeof base64 !== 'string') {
        throw new Error('Base64参数必须是字符串');
    }
    
    // 解析Base64字符串
    const parts = base64.split(';base64,');
    if (parts.length !== 2) {
        throw new Error('Base64格式不正确，应该包含data:image/...;base64,前缀');
    }
    
    // 提取MIME类型和扩展名
    const mimeType = parts[0].split(':')[1];
    const base64Data = parts[1];
    
    // 确定文件扩展名
    let extension = '.png'; // 默认扩展名
    if (mimeType.includes('jpeg') || mimeType.includes('jpg')) {
        extension = '.jpg';
    } else if (mimeType.includes('gif')) {
        extension = '.gif';
    } else if (mimeType.includes('webp')) {
        extension = '.webp';
    } else if (mimeType.includes('bmp')) {
        extension = '.bmp';
    } else if (mimeType.includes('svg')) {
        extension = '.svg';
    }
    
    // 解码Base64数据
    const byteCharacters = atob(base64Data);
    const byteArrays = [];
    
    // 分块处理数据（提高大文件处理性能）
    for (let offset = 0; offset < byteCharacters.length; offset += 512) {
        const slice = byteCharacters.slice(offset, offset + 512);
        
        const byteNumbers = new Array(slice.length);
        for (let i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }
        
        const byteArray = new Uint8Array(byteNumbers);
        byteArrays.push(byteArray);
    }
    
    // 创建Blob对象
    const blob = new Blob(byteArrays, { type: mimeType });
    
    // 创建File对象
    const file = new File([blob], filename + extension, {
        type: mimeType,
        lastModified: lastModified
    });
    
    return file;
}